/* Copyright (c) [2025] [zhi1208@126.com]
 * SPDX-License-Identifier: MIT
 */

/*********************
 *      INCLUDES
 *********************/
#include "hmi_tinyttf.h"
#include "hmi_png.h"
#include "hmi_pg_navigation.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void back_ev_cb(lv_event_t *e);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_font_t *top_bar_title_font;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
lv_obj_t *hmi_top_bar_create(lv_obj_t *parent, const char *title)
{
    lv_obj_t *top_bar = lv_obj_create(parent);
    lv_obj_remove_style_all(top_bar);
    lv_obj_set_size(top_bar, lv_pct(100), lv_pct(15));

    if (!top_bar_title_font)
        top_bar_title_font = hmi_tiny_ttf_create_static("def_font", 24, 0);

    lv_obj_t *title_ = lv_label_create(top_bar);
    lv_obj_set_style_text_color(title_, lv_color_white(), 0);
    lv_obj_set_style_text_font(title_, top_bar_title_font, 0);
    lv_obj_set_style_pad_all(title_, 10, 0);
    lv_obj_center(title_);
#if (LVGL_VERSION_MAJOR >= 9 && LVGL_VERSION_MINOR >= 4 && LVGL_VERSION_PATCH >= 0)
    lv_label_set_text(title_, lv_tr(title));
#else
    lv_label_set_text(title_, title);
#endif

    lv_obj_t *back_wrap = lv_obj_create(top_bar);
    lv_obj_remove_style_all(back_wrap);
    lv_obj_set_size(back_wrap, 40, 40);
    lv_obj_set_style_radius(back_wrap, LV_RADIUS_CIRCLE, 0);
    lv_obj_set_style_bg_color(back_wrap, lv_palette_lighten(LV_PALETTE_GREY, 3), 0);
    lv_obj_set_style_bg_color(back_wrap, lv_palette_main(LV_PALETTE_GREY), LV_STATE_PRESSED);
    lv_obj_set_style_bg_opa(back_wrap, LV_OPA_50, 0);
    lv_obj_add_flag(back_wrap, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(back_wrap, back_ev_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_align(back_wrap, LV_ALIGN_LEFT_MID, 20, 0);

    lv_obj_t *back = lv_image_create(back_wrap);
    lv_image_set_src(back, hmi_png("chevron_left"));
    lv_obj_center(back);

    return top_bar;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void back_ev_cb(lv_event_t *e)
{
    LV_UNUSED(e);
    hmi_pg_pop();
}